import 'package:dio/dio.dart';
import 'package:get/get.dart';
import 'base_service.dart';

/// 通道管理服务类 - GetX服务
class ChannelService extends GetxService {
  final BaseService _baseService = BaseService();

  /// 获取通道数统计
  Future<Map<String, dynamic>> getChannelStatistics() async {
    try {
      final response = await _baseService.get<Map<String, dynamic>>(
        '/channel/channelStat',
      );

      if (response.data != null) {
        final data = response.data!;
        if (data['code'] == 0 && data['success'] == true) {
          return data;
        } else {
          throw Exception(data['msg'] ?? '获取通道数统计失败');
        }
      } else {
        throw Exception('获取通道数统计响应数据为空');
      }
    } on DioException catch (e) {
      print('获取通道数统计网络错误: ${e.message}');
      throw Exception('网络环境不佳，请稍后尝试');
    } catch (e) {
      print('获取通道数统计错误: $e');
      throw Exception('网络环境不佳，请稍后尝试');
    }
  }

  /// 获取域列表
  Future<Map<String, dynamic>> getDomainList({
    String? id,
    String? name,
    int? online,
  }) async {
    try {
      final queryParams = <String, dynamic>{};

      if (id != null && id.isNotEmpty) {
        queryParams['id'] = id;
      }
      if (name != null && name.isNotEmpty) {
        queryParams['name'] = name;
      }
      if (online != null) {
        queryParams['online'] = online;
      }

      final response = await _baseService.post<Map<String, dynamic>>(
        '/domain/list',
        data: queryParams,
      );

      if (response.data != null) {
        final data = response.data!;
        if (data['code'] == 0 && data['success'] == true) {
          return data;
        } else {
          throw Exception(data['msg'] ?? '获取域列表失败');
        }
      } else {
        throw Exception('获取域列表响应数据为空');
      }
    } on DioException catch (e) {
      print('获取域列表网络错误: ${e.message}');
      throw Exception('网络环境不佳，请稍后尝试');
    } catch (e) {
      print('获取域列表错误: $e');
      throw Exception('网络环境不佳，请稍后尝试');
    }
  }

  /// 获取通道列表（分页）
  Future<Map<String, dynamic>> getChannelList({
    String? organizeId,
    String? domainId,
    String? status, // ON|OFF|null(全部)
    String? queryParam, // 通道名称/通道编码，模糊查询
    int pageNo = 1,
    int pageSize = 10,
  }) async {
    try {
      final requestData = <String, dynamic>{
        'pageNo': pageNo,
        'pageSize': pageSize,
      };

      if (organizeId != null && organizeId.isNotEmpty) {
        requestData['organizeId'] = organizeId;
      }
      if (domainId != null && domainId.isNotEmpty) {
        requestData['domainId'] = domainId;
      }
      if (status != null && status.isNotEmpty) {
        requestData['status'] = status;
      }
      if (queryParam != null && queryParam.isNotEmpty) {
        requestData['queryParam'] = queryParam;
      }

      final response = await _baseService.post<Map<String, dynamic>>(
        '/channel/pageChannel',
        data: requestData,
      );

      if (response.data != null) {
        final data = response.data!;
        if (data['code'] == 0 && data['success'] == true) {
          return data;
        } else {
          throw Exception(data['msg'] ?? '获取通道列表失败');
        }
      } else {
        throw Exception('获取通道列表响应数据为空');
      }
    } on DioException catch (e) {
      print('获取通道列表网络错误: ${e.message}');
      throw Exception('网络环境不佳，请稍后尝试');
    } catch (e) {
      print('获取通道列表错误: $e');
      throw Exception('网络环境不佳，请稍后尝试');
    }
  }

  /// 获取组织列表
  Future<Map<String, dynamic>> getOrganizeList() async {
    try {
      final response = await _baseService.get<Map<String, dynamic>>(
        '/channel/listOrganize',
      );

      if (response.data != null) {
        final data = response.data!;
        if (data['code'] == 0 && data['success'] == true) {
          return data;
        } else {
          throw Exception(data['msg'] ?? '获取组织列表失败');
        }
      } else {
        throw Exception('获取组织列表响应数据为空');
      }
    } on DioException catch (e) {
      print('获取组织列表网络错误: ${e.message}');
      throw Exception('网络环境不佳，请稍后尝试');
    } catch (e) {
      print('获取组织列表错误: $e');
      throw Exception('网络环境不佳，请稍后尝试');
    }
  }

  /// 获取通道详情
  Future<Map<String, dynamic>> getChannelDetail(String hsId) async {
    try {
      final response = await _baseService.get<Map<String, dynamic>>(
        '/channel/getChannelDetail?hsId=$hsId',
      );

      if (response.data != null) {
        final data = response.data!;
        if (data['code'] == 0 && data['success'] == true) {
          return data;
        } else {
          throw Exception(data['msg'] ?? '获取通道详情失败');
        }
      } else {
        throw Exception('获取通道详情响应数据为空');
      }
    } on DioException catch (e) {
      print('获取通道详情网络错误: ${e.message}');
      throw Exception('网络环境不佳，请稍后尝试');
    } catch (e) {
      print('获取通道详情错误: $e');
      throw Exception('网络环境不佳，请稍后尝试');
    }
  }
}
